package com.itheima.article.controller;

import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.pojo.ApAuthor;
import com.itheima.article.service.ApArticleService;
import com.itheima.article.service.ApAuthorService;
import com.itheima.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ApiController {
    //注入对应的service接口
    @Autowired
    private ApAuthorService apAuthorService;

    @Autowired
    private ApArticleService apArticleService;

    /**
     * 通过用户id和自媒体账号id获取作者信息
     */
    @GetMapping("/api/apAuthor/{apUserId}/{wmUserId}")
    ResultVo<ApAuthor> getByApUserIdAndWmUserId(
            @PathVariable(name = "apUserId") Long apUserId,
            @PathVariable(name = "wmUserId") Long wmUserId){
        ApAuthor apAuthor = apAuthorService.getByApUserIdAndWmUserId(apUserId,wmUserId);
        return ResultVo.ok(apAuthor);
    }

    /**
     * 创建作者账号
     */
    @PostMapping("/api/apAuthor/add")
    ResultVo add(@RequestBody ApAuthor apAuthor){
        apAuthorService.save(apAuthor);
        return ResultVo.ok();
    }

    /**
     * 增加文章信息
     */
    @PostMapping("/api/apArticle/add")
    ResultVo add(@RequestBody ApArticle apArticle){
        apArticleService.save(apArticle);
        return ResultVo.ok();
    }

    /**
     * 同步自媒体文章到文章微服
     */
    @PostMapping("/api/apArticle/saveArticleInfo")
    ResultVo saveArticleInfo(@RequestBody ArticleInfoDto dto){
        apArticleService.saveArticleInfo(dto);
        return ResultVo.ok();
    }

}
